package com.tiem_files.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

public class BaseController extends HttpServlet {

    protected String getUserId(HttpServletRequest req){
        //获取当前登录的用户ID
        return req.getSession().getAttribute("myUserId").toString();
    }

    protected String getUserMame(HttpServletRequest req){
        //获取当前登录的用户名
        Map map = (Map) req.getSession().getAttribute("userBaseInfo");
        return map.get("username").toString();
    }


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            String uri = req.getRequestURI();
            String fName = uri.substring(uri.lastIndexOf("/") + 1);
            Class<? extends BaseController> aClass = this.getClass();//this:当前引入进来的入口
            //反射代理执行
            Method method = aClass.getDeclaredMethod(fName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(aClass.newInstance(),req,resp);

        }catch (Exception e){
          e.printStackTrace();
        }
    }
}
